﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Runtime.Remoting.Messaging;

namespace WFCSharp_II
{
    public partial class AsynchForm : Form
    {
        private delegate int Calculator(DateTime birthday);

        public AsynchForm()
        {
            InitializeComponent();
        }

        private void calculatorButton_Click(object sender, EventArgs e)
        {
            Calculator countDay = new Calculator(CountDay);
            IAsyncResult result = countDay.BeginInvoke(
                dateTimePicker1.Value, new AsyncCallback(ShowDay), null);
        }

        private void ShowDay(IAsyncResult result)
        {
            Calculator countDay = (Calculator)((AsyncResult)result).AsyncDelegate;
            int value = countDay.EndInvoke(result);
            label2.Text = "Bạn đã sống được " + value + " ngày";
        }
        private int CountDay(DateTime bornedDay)
        {
            Thread.Sleep(5000);
            return (DateTime.Today - dateTimePicker1.Value).Days;
        }
    }
}
